package de.ubschmidt.capi;

import de.ubschmidt.capi.util.Bits;
import de.ubschmidt.capi.util.ByteBufferFactory;
import java.nio.ByteBuffer;

/**
 * This utility class simplifies the access to the details of the
 * binary CAPI profile structure.
 */
public class CapiProfile {

    private ByteBuffer _byteBuffer;

    /**
     *
     * @param buf The binary CAPI profile structure, an array of 64 bytes.
     */
    public CapiProfile(final byte [] buf) {
        _byteBuffer = ByteBufferFactory.wrap(buf);
    }

    /**
     * Return the number of installed controllers.
     *
     * @see section 4.2.2.7
     *
     * @return Number of installed controllers.
     */
    public int getNumberOfInstalledControllers() {
        return _byteBuffer.getShort(0);
    }

    /**
     * @return
     */
    public int getNumberOfSupportedBChannels() {
        return _byteBuffer.getShort(2);
    }

    private int getGlobalOptions() {
        return _byteBuffer.getInt(4);
    }

    private int getB1ProtocolSupport() {
        return _byteBuffer.getInt(8);
    }

    private int getB2ProtocolSupport() {
        return _byteBuffer.getInt(12);
    }

    private int getB3ProtocolSupport() {
        return _byteBuffer.getInt(16);
    }

    /**
     *
     * @return
     */
    public boolean isInternalControllerSupported() {
        return Bits.isBitSet(getGlobalOptions(), 0);
    }


    /**
     *
     * @return
     */
    public boolean isExternalEquipmentSupported() {
        return Bits.isBitSet(getGlobalOptions(), 1);
    }

    /**
     *
     * @return
     */
    public boolean isHandsetSupported() {
        return Bits.isBitSet(getGlobalOptions(), 2);
    }

    /**
     *
     * @return
     */
    public boolean isDtmfSupported() {
        return Bits.isBitSet(getGlobalOptions(), 3);
    }

    /**
     *
     * @return
     */
    public boolean isSupplementaryServicesSupported() {
        return Bits.isBitSet(getGlobalOptions(), 4);
    }

    /**
     *
     * @return
     */
    public boolean isChannelAllocationSupported() {
        return Bits.isBitSet(getGlobalOptions(), 5);
    }

    /**
     *
     * @return
     */
    public boolean isParameterBChannelOperationSupported() {
        return Bits.isBitSet(getGlobalOptions(), 6);
    }

    /**
     *
     * @return
     */
    public boolean isLineInterconnectSupported() {
        return Bits.isBitSet(getGlobalOptions(), 7);
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append(getClass().getSimpleName());
        sb.append("[numCtrls=").append(getNumberOfInstalledControllers());
        sb.append(" numChans=").append(getNumberOfSupportedBChannels());
        sb.append(" globalOptions=").append(getGlobalOptions());
        sb.append(" b1=").append(getB1ProtocolSupport());
        sb.append(" b2=").append(getB2ProtocolSupport());
        sb.append(" b3=").append(getB3ProtocolSupport());
        sb.append(" intCtrl=").append(isInternalControllerSupported());
        sb.append(" extEquip=").append(isExternalEquipmentSupported());
        sb.append(" handset=").append(isHandsetSupported());
        sb.append(" dtmf=").append(isDtmfSupported());
        sb.append(" supplServ=").append(isSupplementaryServicesSupported());
        sb.append(" ...");
        sb.append("]");

        return sb.toString();
    }
}
